#include "iface.h"
#include "resources.h"

#include <string.h>

CLog wrn = CLog( CLog::WRN );
CLog err = CLog( CLog::ERR );
CLog inf = CLog( CLog::INF );

bool CIface::is_static_init	= false;
ifont* CIface::arial8n 	= NULL;
ifont* CIface::arial12 	= NULL;
ifont* CIface::arialb12 = NULL;
ifont* CIface::cour16 	= NULL;
ifont* CIface::cour24 	= NULL;
ifont* CIface::times20 	= NULL;
pbcRect* CIface::update_areas = NULL;
int CIface::n_update_areas = 0;

const ibitmap* CIface::card_images[4][14];



void CIface::AddUpdateArea( pbcRect _rect )
{
		pbcRect* tmp = new pbcRect[n_update_areas+1];
		if( n_update_areas )
		{
			memcpy( tmp, update_areas, sizeof( pbcRect ) * n_update_areas );
			delete update_areas;
		}
		tmp[n_update_areas] = _rect;
		update_areas = tmp;
		n_update_areas++;
}

void CIface::UpdateAreas()
{
	if( n_update_areas )
	{
		for ( int i = 0; i < n_update_areas; i++ )
		{
			::PartialUpdate( update_areas[i].x, update_areas[i].y, update_areas[i].width, update_areas[i].height );
		}
		delete update_areas;
		n_update_areas = 0;
	}
}

void CIface::InitStatic()
{
	//std::cout << inf << "Called static intialization" << std::endl;

	if( !is_static_init )
	{
		std::cout << inf << "Load fonts!" << std::endl;
		arial8n = OpenFont("LiberationSans", 8, 0);
		arial12 = OpenFont("LiberationSans", 12, 1);
		arialb12 = OpenFont("LiberationSans", 12, 1);
		cour16 = OpenFont("LiberationMono", 16, 1);
		cour24 = OpenFont("LiberationMono", 24, 1);
		times20 = OpenFont("LiberationSerif", 20, 1);	
		
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_ACE] = &cimg_club_01;
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_TWO] = &cimg_club_02;
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_THREE] = &cimg_club_03;
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_FOUR] = &cimg_club_04;
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_FIVE] = &cimg_club_05;
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_SIX] = &cimg_club_06;
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_SEVEN] = &cimg_club_07;
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_EIGHT] = &cimg_club_08;
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_NINE] = &cimg_club_09;
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_TEN] = &cimg_club_10;
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_JACKE] = &cimg_club_11;
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_QUEEN] = &cimg_club_12;
		card_images[CARD_INDEX_CLUBS][CARD_INDEX_KING] = &cimg_club_13;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_ACE] = &cimg_diamond_01;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_TWO] = &cimg_diamond_02;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_THREE] = &cimg_diamond_03;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_FOUR] = &cimg_diamond_04;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_FIVE] = &cimg_diamond_05;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_SIX] = &cimg_diamond_06;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_SEVEN] = &cimg_diamond_07;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_EIGHT] = &cimg_diamond_08;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_NINE] = &cimg_diamond_09;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_TEN] = &cimg_diamond_10;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_JACKE] = &cimg_diamond_11;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_QUEEN] = &cimg_diamond_12;
		card_images[CARD_INDEX_DIAMONDS][CARD_INDEX_KING] = &cimg_diamond_13;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_ACE] = &cimg_heart_01;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_TWO] = &cimg_heart_02;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_THREE] = &cimg_heart_03;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_FOUR] = &cimg_heart_04;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_FIVE] = &cimg_heart_05;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_SIX] = &cimg_heart_06;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_SEVEN] = &cimg_heart_07;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_EIGHT] = &cimg_heart_08;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_NINE] = &cimg_heart_09;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_TEN] = &cimg_heart_10;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_JACKE] = &cimg_heart_11;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_QUEEN] = &cimg_heart_12;
		card_images[CARD_INDEX_HEARTS][CARD_INDEX_KING] = &cimg_heart_13;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_ACE] = &cimg_spade_01;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_TWO] = &cimg_spade_02;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_THREE] = &cimg_spade_03;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_FOUR] = &cimg_spade_04;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_FIVE] = &cimg_spade_05;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_SIX] = &cimg_spade_06;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_SEVEN] = &cimg_spade_07;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_EIGHT] = &cimg_spade_08;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_NINE] = &cimg_spade_09;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_TEN] = &cimg_spade_10;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_JACKE] = &cimg_spade_11;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_QUEEN] = &cimg_spade_12;
		card_images[CARD_INDEX_SPADES][CARD_INDEX_KING] = &cimg_spade_13;		
		
		is_static_init = true;
	}
}

CIface::CIface()
{
	InitStatic();
}

void CIface::DrawCard( int _suit, int _index, int _x, int _y )
{
	
	std::cout << inf << "CIface::DrawCard: " << _x << "," << _y << "," << _suit << "," << _index <<std::endl;
	
	if( _suit > 4 or _suit < 0 or _index < 0 or _index > 13 )
	{
		std::cout << err << "CIface::DrawCard: card index out of range " << _suit << "," << _index <<std::endl;
		return;
	}
	
	DrawBitmap( _x, _y, card_images[_suit][_index] );
} 

void CIface::DrawPlaceHolder( int _x, int _y )
{
	DrawBitmap( _x, _y, &cimg_placeholder );
}

void CIface::DrawHand( int _x, int _y )
{
	DrawBitmap( _x, _y, &cimg_hand );
}

void CIface::DrawBack( int _x, int _y )
{
	DrawBitmap( _x, _y, &cimg_back );
}

void CIface::DrawText( const char* _text, unsigned int _x, unsigned int _y, ifont* _font )
{
	char text[ 61 ];
	text[60] = 0;

	ifont* font = _font;
	if( font == NULL ) font = CIface::cour16;
	if( font == NULL ) 
	{
		std::cout << wrn << "Fonts are not initialized!" << std::endl;
		return;
	}
	if( _text == NULL or strlen( _text ) == 0 )
	{
		std::cout << wrn << "nothing to do, so exit" << std::endl;
		return;
	}

	strncpy( text, _text, 60 );
	
	std::cout << inf << "DrawText: " << _text << " " << _x << " " << _y << std::endl;	

	SetFont( font, BLACK);
	DrawString(_x, _y, _text);
}
